﻿using CatUtils.Utils.Currency.TextUtils;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace CatUtils.Utils.Currency.IoUtils
{/// <summary>
 /// 文件夹名字帮助
 /// </summary>
    public static class FloderNameHelper
    {/// <summary>
     /// 文本结束符
     /// </summary>
        private const string EndSymbol = "\\";

        /// <summary>
        /// 获取文件夹路径  以\\结尾
        /// </summary>
        /// <param name="Floderpath"></param>
        /// <returns></returns>
        public static string GetFloderPath(string Floderpath)
        {
            try { Floderpath = Path.GetDirectoryName(Floderpath) + EndSymbol; }
            catch (Exception ex)
            {
                PrintLog.Log(ex);
            }
            return Floderpath;
        }

        /// <summary>
        /// 获取文件夹路径  以\\结尾
        /// </summary>
        /// <param name="Floderpath"></param>
        /// <returns></returns>
        public static string GetParentFloderPath(string Floderpath)
        {
            try
            {
                Floderpath = GetFloderPath(Floderpath);
                Floderpath = Directory.GetParent(Floderpath).FullName;
            }
            catch (Exception ex)
            {
                PrintLog.Log(ex);
            }
            return Floderpath;
        }

        /// <summary>
        /// 获取文件夹名字 例如：c://cat// 返回cat
        /// </summary>
        /// <param name="Floderpath"></param>
        /// <returns></returns>
        public static string GetFloderName(string Floderpath)
        {
            try
            {
                Floderpath = GetFloderPath(Floderpath);
                Floderpath = Floderpath.Substring(0, Floderpath.Length - 1);
            }
            catch (Exception ex)
            {
                Console.WriteLine("获取文件名不正确");
                PrintLog.Log(ex);
            }
            return FileNameHelper.GetFileName(Floderpath);
        }

        /// <summary>
        /// 创建一个文件夹路径
        /// </summary>
        /// <param name="ParentFloder">父路径，以\\结尾</param>
        /// <param name="ChildFloderName">子路径，不需要\\，直接写“FloderName”即可</param>
        /// <param name="CreateFlag">是否创建文件夹</param>
        /// <returns></returns>
        public static string CreateFloderPath(string ParentFloder, string ChildFloderName, bool CreateFlag = true)
        {
            if (ParentFloder.LastIndexOf(EndSymbol) != ParentFloder.Length - 1)
            {
                //如果不以\\结尾，则代表这个路径没有封闭，先进行封闭
                System.Console.WriteLine("父路径未封闭，请注意以\\进行结尾");
                ParentFloder = ParentFloder + EndSymbol;
            }
            ParentFloder = ParentFloder + TextHelper.ReplaceBadChar(ChildFloderName) + EndSymbol;
            if (CreateFlag)
                FloderHelper.FolderCreate(ParentFloder);

            return ParentFloder;
        }

        /// <summary>
        /// 获取目录下文件列表，不带路径
        /// </summary>
        /// <param name="FloderPath"></param>
        /// <param name="Pattern"></param>
        /// <returns></returns>
        public static List<string> GetFileNameList(string FloderPath, string Pattern = "*.*")
        {
            List<string> FileList = GetFullFileNameList(FloderPath, Pattern);
            List<string> TempList = new List<string>();
            foreach (string Line in FileList)
            {
                TempList.Add(FileNameHelper.GetFileName(Line));
            }
            return TempList;
        }

        /// <summary>
        /// 获取Img图像列表，若启用增强模式，则获取不常用文件类型
        /// </summary>
        /// <param name="FloderPath"></param>
        /// <param name="Enhance">是否打开增强模式</param>
        /// <returns></returns>
        public static List<string> GetFullImgList(string FloderPath, bool Enhance)
        {
            List<string> TemplList = GetFullFileNameList(FloderPath, "*.jpg");
            TemplList.AddRange(GetFullFileNameList(FloderPath, "*.png"));
            if (Enhance)
            {
                TemplList.AddRange(GetFullFileNameList(FloderPath, "*.bmp"));
                TemplList.AddRange(GetFullFileNameList(FloderPath, "*.gif"));
                TemplList.AddRange(GetFullFileNameList(FloderPath, "*.jpeg"));
            }

            return TemplList;
        }

        /// <summary>
        /// 获取Img图像列表
        /// </summary>
        /// <param name="FloderPath"></param>

        /// <returns></returns>
        public static List<string> GetFullTextList(string FloderPath)
        {
            List<string> TemplList = GetFullFileNameList(FloderPath, "*.txt");
            TemplList.AddRange(GetFullFileNameList(FloderPath, "*.lst"));

            return TemplList;
        }

        /// <summary>
        /// 获取目录下文件列表，带路径
        /// </summary>
        /// <param name="FloderPath"></param>
        /// <param name="Pattern"></param>
        /// <returns></returns>
        public static List<string> GetFullFileNameList(string FloderPath, string Pattern = "*.*")
        {
            List<string> FileList = new List<string>();
            try
            {
                FileList = Directory.GetFiles(FloderPath, Pattern).ToList<string>();
            }
            catch (Exception ex)
            {
                PrintLog.Log("获取文件列表失败，文件夹{0}没有创建。", FloderPath);
                PrintLog.Log(ex);
            }
            return FileList;
        }

        /// <summary>
        /// 获取文件夹列表
        /// </summary>
        /// <param name="FloderPath"></param>
        /// <returns></returns>
        public static List<string> GetFloderNameList(string FloderPath)
        {
            List<string> FloderList = new List<string>();
            if (!FloderHelper.FloderExits(FloderPath, true))
            {
                Console.WriteLine("创建新文件夹失败");
                return FloderList;
            }
            try
            {
                foreach (string SingleFloder in Directory.GetDirectories(FloderPath).ToList<string>())
                {
                    FloderList.Add(SingleFloder + EndSymbol);
                }
            }
            catch (Exception ex)
            {
                PrintLog.Log(ex);
            }
            return FloderList;
        }

        /// <summary>
        /// 创建子文件夹，如C:\floder\ →C:\floder\tip\
        /// </summary>
        /// <param name="FloderPath"></param>
        /// <param name="Tip"></param>
        /// <param name="CreateFlag"></param>
        /// <returns></returns>
        public static string CreateChildFloderPath(string FloderPath, string Tip, bool CreateFlag = true)
        {
            if (Tip == "")
                return FloderPath;

            string NewFloerPath = FloderNameHelper.GetFloderPath(FloderPath) + TextHelper.ReplaceBadChar(Tip) + EndSymbol;
            FloderHelper.FloderExits(NewFloerPath, CreateFlag);
            return NewFloerPath;
        }
    }
}